package com.sora.basic.controller;

import com.sora.basic.dto.PhoneDto;
import com.sora.basic.exception.BusinessException;
import com.sora.basic.service.IVerifyCodeService;
import com.sora.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取前端生成的uuid的key值
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String key){
        try {
            Object base64Str = verifyCodeService.getImageCode(key);
            return new AjaxResult(true,base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }


    /**
     * 手机号码注册获取手机验证码的接口
     * @param phoneDto
     * @return
     */
    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneDto phoneDto){
        try {
            verifyCodeService.getPhoneCode(phoneDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }


    /**
     * 微信三方登录获取手机验证码的接口
     * @param phoneDto
     * @return
     */
    @PostMapping("binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody PhoneDto phoneDto){
        try {
            verifyCodeService.getBinderSmsCode(phoneDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }
}
